package com.example.tao.mycar.activity;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.avos.avoscloud.AVUser;
import com.example.tao.mycar.MyActionBarActivity;
import com.example.tao.mycar.MyApplication;
import com.example.tao.mycar.R;
import com.example.tao.mycar.database.OperateDBUtils;

import java.util.Calendar;

public class InputHeightActivity extends MyActionBarActivity implements View.OnClickListener {

    private Button saveButton;
    private EditText inputHeight;
    private TextView inputDate;

    private DatePickerDialog mDatePickerDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_input_height);
        setToolBar(getResources().getString(R.string.title_activity_input_height), true);

        saveButton = (Button)findViewById(R.id.btn_save);
        inputHeight = (EditText)findViewById(R.id.input_height);
        inputDate = (TextView)findViewById(R.id.txt_height_date_input);

        saveButton.setOnClickListener(this);
        inputDate.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.txt_height_date_input :
                if (mDatePickerDialog==null || !mDatePickerDialog.isShowing()) {
                    Calendar calendar = Calendar.getInstance();
                    mDatePickerDialog = new DatePickerDialog(InputHeightActivity.this, new DatePickerDialog.OnDateSetListener() {
                        @Override
                        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                            inputDate.setText(year + "-" + monthOfYear + "-" + dayOfMonth);
                        }
                    }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH));
                    mDatePickerDialog.show();
                }
                break;
            case R.id.btn_save :
                String height = inputHeight.getText().toString();
                String time = inputDate.getText().toString();
                if (!TextUtils.isEmpty(height) && !TextUtils.isEmpty(time)) {
                    displayRecords(height, time);
                    Log.d(MyApplication.TAG, height);
                    Log.d(MyApplication.TAG, time);
                    finish();
                } else {
                    Toast.makeText(this, "请输入时间和身高！", Toast.LENGTH_LONG).show();
                }

                break;
            default:
                break;
        }
    }

    private void displayRecords(String height, String time) {
        String colums[] = new String[] {
                OperateDBUtils.TIME, OperateDBUtils.HEIGHT
        };
        Uri uri = OperateDBUtils.HEIGHT_URI;
        String selection = OperateDBUtils.TIME + "=? and " + OperateDBUtils.USER_ID + "=?";
        String selectionArgs[] = new String[] {time, AVUser.getCurrentUser().getObjectId()};
        Log.d(MyApplication.TAG, selection);
        Cursor cursor = null;
        try {
            cursor = getContentResolver().query(uri, colums, selection, selectionArgs, null);
            if (cursor != null && cursor.moveToFirst()) {
                updateHeight(height, time);
                Log.d(MyApplication.TAG, "updateHeight");
            } else {
                insertHeight(height, time);
                Log.d(MyApplication.TAG, "insertHeight");
            }
        } catch (Exception e) {
            Log.d(MyApplication.TAG, "displayRecords Exception" + e);
            e.printStackTrace();
        } finally {
            if (cursor != null) {
                cursor.close();
            }
        }
    }

    private void insertHeight(String height, String time) {
        ContentValues contentValues = new ContentValues();
        contentValues.put(OperateDBUtils.TIME, time);
        contentValues.put(OperateDBUtils.HEIGHT, height);
        contentValues.put(OperateDBUtils.USER_ID, AVUser.getCurrentUser().getObjectId());
        getContentResolver().insert(OperateDBUtils.HEIGHT_URI, contentValues);
    }

    private void updateHeight(String height, String time) {
        String selection = OperateDBUtils.TIME + "=? and " + OperateDBUtils.USER_ID + "=?";
        String selectionArgs[] = new String[] {time, AVUser.getCurrentUser().getObjectId()};
        ContentValues contentValues = new ContentValues();
        contentValues.put(OperateDBUtils.TIME, time);
        contentValues.put(OperateDBUtils.HEIGHT, height);
        contentValues.put(OperateDBUtils.USER_ID, AVUser.getCurrentUser().getObjectId());
        getContentResolver().update(OperateDBUtils.HEIGHT_URI, contentValues, selection, selectionArgs);
    }
}
